READ_KEYPAD PROC FAR
;config: (Left) PC0-PC3,PB0-PB2 (Right)
ROW_0: ;1st row 123
	MOV DX,PPI_8255_PORTC
	MOV AL,00Eh ;0000 1110
	OUT DX, AL
	
	MOV DX,PPI_8255_PORTB
	IN AL,DX ;default should be xxxx x111
	AND AL,03fh ;only keep value for 3 collumns

;jump to next row if no key has been pressed
	cmp AL,03fh
	je ROW_1	

;determine which key has been pressed


	cmp AL, 03EH
	JE ONE_ONE

	CMP AL, 03DH
	JE ONE_TWO

	CMP AL, 03BH
	JE ONE_THREE

	CMP AL,037h
	je TWO_ONE

	cmp AL,02Fh
	je TWO_TWO

	jmp TWO_THREE

ONE_ONE:
	mov AL,leftKeyMap[0]
	jmp END_KEY_CHECK
ONE_TWO:
	mov AL,leftKeyMap[1]
	jmp END_KEY_CHECK
ONE_THREE:	
	mov AL,leftKeyMap[2]
	jmp END_KEY_CHECK

TWO_ONE:
	mov AL,rightKeyMap[0]
	jmp END_KEY_CHECK

TWO_TWO:	
	mov AL,rightKeyMap[1]
	jmp END_KEY_CHECK

TWO_THREE:	
	mov AL,rightKeyMap[2]
	jmp END_KEY_CHECK

ROW_1: ;2nd row 456
	MOV DX,PPI_8255_PORTC
	MOV AL,0Dh ;0000 1101
	OUT DX, AL
	
	MOV DX,PPI_8255_PORTB
	IN AL,DX ;default should be xxxx x111
	AND AL,03fh ;only keep value for 3 collumns

;jump to next row if no key has been pressed
	cmp AL,03fh
	je ROW_2	

;determine which key has been pressed
	cmp AL, 00111110b
	JE ONE_FOUR

	CMP AL, 00111101b
	JE ONE_FIVE

	CMP AL, 00111011b
	JE ONE_SIX

	CMP AL, 00110111b
	je TWO_FOUR

	cmp AL, 00101111b
	je TWO_FIVE

	jmp TWO_SIX

ONE_FOUR:
	mov AL,leftKeyMap[3]
	;call far ptr DEBUG_PRINT_AL
	jmp END_KEY_CHECK
ONE_FIVE:
	mov AL,leftKeyMap[4]
	;call far ptr DEBUG_PRINT_AL
	jmp END_KEY_CHECK
ONE_SIX:	
	mov AL,leftKeyMap[5]
	;call far ptr DEBUG_PRINT_AL
	jmp END_KEY_CHECK

TWO_FOUR:
	mov AL,rightKeyMap[3]
	;call far ptr DEBUG_PRINT_AL
	jmp END_KEY_CHECK

TWO_FIVE:	
	mov AL,rightKeyMap[4]
	;call far ptr DEBUG_PRINT_AL
	jmp END_KEY_CHECK

TWO_SIX:	
	mov AL,rightKeyMap[5]
	;call far ptr DEBUG_PRINT_AL
	jmp END_KEY_CHECK

ROW_2: ;3rd row 789
	MOV DX,PPI_8255_PORTC
	MOV AL,0Bh ;0000 1011
	OUT DX, AL
	
	MOV DX,PPI_8255_PORTB
	IN AL,DX ;default should be xxxx x111
	AND AL,03fh ;only keep value for 3 collumns

;jump to next row if no key has been pressed
	cmp AL,03fh
	je ROW_3

;determine which key has been pressed
	cmp AL, 03EH
	JE ONE_SEVEN

	CMP AL, 03DH
	JE ONE_EIGHT

	CMP AL, 03BH
	JE ONE_NINE

	CMP AL,037h
	je TWO_SEVEN

	cmp AL,02Fh
	je TWO_EIGHT

	jmp TWO_NINE

ONE_SEVEN:
	mov AL,leftKeyMap[6]
	;call far ptr DEBUG_PRINT_AL
	jmp END_KEY_CHECK
ONE_EIGHT:
	mov AL,leftKeyMap[7]
	;call far ptr DEBUG_PRINT_AL
	jmp END_KEY_CHECK
ONE_NINE:	
	mov AL,leftKeyMap[8]
	;call far ptr DEBUG_PRINT_AL
	jmp END_KEY_CHECK

TWO_SEVEN:
	mov AL,rightKeyMap[6]
	;call far ptr DEBUG_PRINT_AL
	jmp END_KEY_CHECK

TWO_EIGHT:	
	mov AL,rightKeyMap[7]
	;call far ptr DEBUG_PRINT_AL
	jmp END_KEY_CHECK

TWO_NINE:	
	mov AL,rightKeyMap[8]
	;call far ptr DEBUG_PRINT_AL
	jmp END_KEY_CHECK

ROW_3: ;last row *0#
	MOV DX,PPI_8255_PORTC
	MOV AL,07h ;0000 1011
	OUT DX, AL
	
	MOV DX,PPI_8255_PORTB
	IN AL,DX ;default should be xxxx x111
	AND AL,03fh ;only keep value for 3 collumns

;jump to next row if no key has been pressed
	cmp AL,03fh
	je NO_KEY

;determine which key has been pressed
	cmp AL, 03EH
	JE ONE_STAR

	CMP AL, 03DH
	JE ONE_ZERO

	CMP AL, 03BH
	JE ONE_HASH

	CMP AL,037h
	je TWO_STAR

	cmp AL,02Fh
	je TWO_ZERO

	jmp TWO_HASH

ONE_STAR:
	mov AL,leftKeyMap[9]
	;call far ptr DEBUG_PRINT_AL
	jmp END_KEY_CHECK
ONE_ZERO:
	mov AL,leftKeyMap[10]
	;call far ptr DEBUG_PRINT_AL
	jmp END_KEY_CHECK
ONE_HASH:	
	mov AL,leftKeyMap[11]
	;call far ptr DEBUG_PRINT_AL
	jmp END_KEY_CHECK

TWO_STAR:
	mov AL,rightKeyMap[9]
	;call far ptr DEBUG_PRINT_AL
	jmp END_KEY_CHECK

TWO_ZERO:	
	mov AL,rightKeyMap[10]
	;call far ptr DEBUG_PRINT_AL
	jmp END_KEY_CHECK

TWO_HASH:	
	mov AL,rightKeyMap[11]
	;call far ptr DEBUG_PRINT_AL
	jmp END_KEY_CHECK

NO_KEY:
;determine which key has been pressed
	mov AL,0FFh
	;call far ptr DEBUG_PRINT_AL
	jmp END_KEY_CHECK

END_KEY_CHECK:
	RET
READ_KEYPAD ENDP
